package com.example.carbonbackend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.carbonbackend.constant.CommonConstant;
import com.example.carbonbackend.mapper.ListingInfoMapper;
import com.example.carbonbackend.model.dto.ListingInfo.ListingInfoQueryRequest;
import com.example.carbonbackend.model.entity.ListingInfo;
import com.example.carbonbackend.model.vo.ListingInfoVO;
import com.example.carbonbackend.service.ListingInfoService;
import com.example.carbonbackend.service.UserService;
import com.example.carbonbackend.utils.SqlUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author b'j'x
 * @description 针对表【listinginfo(挂牌信息表)】的数据库操作Service实现
 * @createDate 2024-03-28 14:14:28
 */
@Service
public class ListingInfoServiceImpl extends ServiceImpl<ListingInfoMapper, ListingInfo>
        implements ListingInfoService {

    @Resource
    private UserService userService;

    /**
     * 封装查询条件
     *
     * @param listingInfoQueryRequest
     * @return
     */
    @Override
    public QueryWrapper<ListingInfo> getQueryWrapper(ListingInfoQueryRequest listingInfoQueryRequest) {
        QueryWrapper<ListingInfo> queryWrapper = new QueryWrapper<>();
        if (listingInfoQueryRequest == null) {
            return queryWrapper;
        }
        Long id = listingInfoQueryRequest.getId();
        String companyId = listingInfoQueryRequest.getCompanyId();
        Double listingPrice = listingInfoQueryRequest.getListingPrice();
        Double listingAmount = listingInfoQueryRequest.getListingAmount();
        Double residualAmount = listingInfoQueryRequest.getResidualAmount();
        String sellerAddress = listingInfoQueryRequest.getSellerAddress();
        Double maxPrice = listingInfoQueryRequest.getMaxPrice();
        Double minPrice = listingInfoQueryRequest.getMinPrice();
        Integer type = listingInfoQueryRequest.getType();
        Date startTime = listingInfoQueryRequest.getStartTime();
        Date endTime = listingInfoQueryRequest.getEndTime();

        String sortField = listingInfoQueryRequest.getSortField();
        String sortOrder = listingInfoQueryRequest.getSortOrder();


        // 拼接查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(companyId), "company_id", companyId);
        queryWrapper.eq(ObjectUtils.isNotEmpty(id), "id", id);
        queryWrapper.eq(ObjectUtils.isNotEmpty(listingPrice), "listing_price", listingPrice);
        queryWrapper.eq(ObjectUtils.isNotEmpty(listingAmount), "listing_amount", listingAmount);
        queryWrapper.eq("is_delete", false);
        queryWrapper.le(ObjectUtils.isNotEmpty(startTime), "listingInfo_time", startTime);
        queryWrapper.ge(ObjectUtils.isNotEmpty(endTime), "listingInfo_time", endTime);
        queryWrapper.eq(ObjectUtils.isNotEmpty(residualAmount), "residual_amount", residualAmount);
        queryWrapper.eq(ObjectUtils.isNotEmpty(sellerAddress), "seller_address", sellerAddress);
        queryWrapper.eq(ObjectUtils.isNotEmpty(maxPrice), "max_price", maxPrice);
        queryWrapper.eq(ObjectUtils.isNotEmpty(minPrice), "min_price", minPrice);

        queryWrapper.eq(ObjectUtils.isNotEmpty(type), "type", type);

        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);
        return queryWrapper;
    }

    /**
     * 清除敏感消息
     *
     * @param listingInfo
     * @param request
     * @return
     */
    @Override
    public ListingInfoVO getListingInfoVO(ListingInfo listingInfo, HttpServletRequest request) {
        if (listingInfo == null) {
            return null;
        }

        ListingInfoVO listingInfoVO = new ListingInfoVO();
        BeanUtils.copyProperties(listingInfo, listingInfoVO);

        return listingInfoVO;
    }

    @Override
    public Page<ListingInfoVO> getListingInfoVOPage(Page<ListingInfo> listingInfoPage, HttpServletRequest request) {
        List<ListingInfo> listingInfoList = listingInfoPage.getRecords();
        Page<ListingInfoVO> listingInfoVOPage = new Page<>(listingInfoPage.getCurrent(), listingInfoPage.getSize(), listingInfoPage.getTotal());

        if (CollectionUtils.isEmpty(listingInfoList)) {
            return listingInfoVOPage;
        }

        List<ListingInfoVO> collect = listingInfoList.stream().map(listingInfo -> {

            ListingInfoVO listingInfoVO = new ListingInfoVO();

            BeanUtils.copyProperties(listingInfo,listingInfoVO);

                return listingInfoVO;


        }).collect(Collectors.toList());

        listingInfoVOPage.setRecords(collect);

        return listingInfoVOPage;
    }
}




